package lu.uni.adtool.domains;

import java.io.Serializable;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Set;

import lu.uni.adtool.adtree.ADTNode;
import lu.uni.adtool.adtree.Node;

/**
 * Assignment map that associates labels of the nodes with domain valuations.
 * 
 * @author Piot Kordy
 * @version 
 */
public class ValueAssignement<T> implements Serializable
{
  static final long serialVersionUID = 975147854358646403L;
  /**
   * Hash map for internal storage.
   */
  private HashMap<String, T> map;

  /**
   * {@inheritDoc}
   * @see Object#ValueAssignement()
   */
  public ValueAssignement()
  {
    map = new HashMap<String, T>(32);
  }

  /**
   * Assigns value to a label.
   * 
   * @param value
   * @param label
   */
  public void put(final String label, final T value)
  {
    map.put(label, value);
  }

  /**
   * Remove the label from the map.
   * 
   * @param label label to be removed.
   */
  public final void remove(final String label)
  {
    map.remove(label);
  }

  /**
   * Get the value for the given key.
   * 
   * @param label label of the key
   * @return key
   */
  public final T get(final String label)
  {
    return map.get(label);
  }

  /**
   * Sets the default value for labels that do not have value set.
   * 
   * @param proponent when true we create value map for proponent and when
   * false for opponent type of nodes.
   * @param root root of the tree.
   * @param domain domain
   */
  public final void setDefault(final boolean proponent, final ADTNode root,
      final Domain<T> domain)
  {
    final ADTNode.Type type = root.getType();
    if (type == ADTNode.Type.LEAFO && !proponent){
      if (map.get(root.getName()) == null) {
        map.put(root.getName(),
            domain.getDefaultValue(type == ADTNode.Type.LEAFO));
      }
    }
    else if(type == ADTNode.Type.LEAFP && proponent) {
      if (map.get(root.getName()) == null) {
        map.put(root.getName(),
            domain.getDefaultValue(type == ADTNode.Type.LEAFP));
      }
    }
    else {
      if (root.getChildren() != null) {
        for(Node c:root.getChildren()){
          if (c != null) {
            setDefault(proponent,(ADTNode)c, domain);
          }
        }
      }
    }
  }
  public Set<String> keySet()
  {
    return map.keySet();
  }
}

